package com.chs.recoginition.vo;

import com.chs.recoginition.core.exception.ApplicationException;
import com.chs.recoginition.core.exception.ApplicationExceptionMessage;
import lombok.Data;
import static com.chs.recoginition.core.exception.constant.ExceptionConstant.SUCCESS;

@Data
public class Result<T> {

    /**
     * 错误代码
     */
    private String errCode;

    /**
     * 错误信息
     */
    private String errMsg;

    /**
     * 返回对象
     */
    private T data;

    private Result() {
    }

    private Result(T data) {
        this.data = data;
    }

    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>(data);
        result.setErrCode(SUCCESS.getErrorCode());
        result.setErrMsg(SUCCESS.getErrorMessage());
        return result;
    }

    public static <T> Result<T> success() {
        return success(null);
    }

    public static Result error(ApplicationExceptionMessage exceptionMessage) {
        return error(exceptionMessage.getErrorCode(), exceptionMessage.getErrorMessage());
    }

    public static Result error(ApplicationException oaException) {
        return error(oaException.getErrorCode(), oaException.getErrorMessage());
    }

    public static Result error(String errCode, String errMsg) {
        Result result = new Result();
        result.setErrCode(errCode);
        result.setErrMsg(errMsg);
        return result;
    }

}
